[Clang] diagnosing missing Vulkan environment when using SPIR-V triple#190840
[Clang] diagnosing missing Vulkan environment when using SPIR-V triple#190840
Conversation
…V target for release When a user passes '-target spirv' without specififying a vulkan environment ttriple, SPIRVTargetInfo will fire an assert instead of throwing an error diagnostic. Added this diagnostic in CompilerInstance::createTarget() before target is initialized. Fixes llvm#189964
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-driver Author: PrabbyDD ChangesFixes issue #189964 Full diff: https://github.com/llvm/llvm-project/pull/190840.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 62b74574102e4..0a3e4e82a79e5 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -43,6 +43,9 @@ def note_fe_backend_resource_limit: Note<"%0 (%1) exceeds limit (%2) in '%3'">,
def remark_fe_backend_plugin: Remark<"%0">, BackendInfo, InGroup<RemarkBackendPlugin>;
def note_fe_backend_plugin: Note<"%0">, BackendInfo;
+def err_spirv_requires_vulkan : Error<
+ "SPIR-V target requires a Vulkan environment (e.g. '-target spirv64-unknown-vulkan1.3')">;
+
def warn_fe_override_module : Warning<
"overriding the module target triple with %0">,
InGroup<DiagGroup<"override-module">>;
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 0b00ad7128c00..89898d3adfbae 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -112,6 +112,17 @@ void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
bool CompilerInstance::createTarget() {
+
+ // Validate Vulkan environment for SPIRV.
+ llvm::Triple Triple(getInvocation().getTargetOpts().Triple);
+ if (Triple.getArch() == llvm::Triple::spirv) {
+ if (Triple.getOS() != llvm::Triple::Vulkan ||
+ Triple.getVulkanVersion() == llvm::VersionTuple(0)) {
+ getDiagnostics().Report(diag::err_spirv_requires_vulkan) << Triple.str();
+ return false;
+ }
+ }
+
// Create the target instance.
setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
getInvocation().getTargetOpts()));
diff --git a/clang/test/Driver/spirv-target-validation.c b/clang/test/Driver/spirv-target-validation.c
new file mode 100644
index 0000000000000..cde5b46c54b94
--- /dev/null
+++ b/clang/test/Driver/spirv-target-validation.c
@@ -0,0 +1,4 @@
+// RUN: %clang -target spirv %s 2>&1 | FileCheck %s
+// CHECK: error: SPIR-V target requires a Vulkan environment
+
+int main() { return 0; }
\ No newline at end of file
|
|
Hello! This is my first PR to LLVM. A small diagnostics fix. If there is any advice for newbie mistakes I might have made, I will gladly fix them. I am trying to work my way up from smaller issues to larger ones in clang because it is complex. I tried to include short but meaningful comments in the code, a unit test that mimics others I saw, and meaningful commit messages. Thanks. |
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions c,cpp -- clang/test/Driver/spirv-target-validation.c clang/lib/Frontend/CompilerInstance.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 89898d3ad..b51f9027b 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -113,7 +113,7 @@ void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
bool CompilerInstance::createTarget() {
- // Validate Vulkan environment for SPIRV.
+ // Validate Vulkan environment for SPIRV.
llvm::Triple Triple(getInvocation().getTargetOpts().Triple);
if (Triple.getArch() == llvm::Triple::spirv) {
if (Triple.getOS() != llvm::Triple::Vulkan ||
|
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) ClangClang.Driver/spirv-target-validation.clldb-apilldb-api.commands/process/attach/TestProcessAttach.pyIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
🪟 Windows x64 Test Results
Failed Tests(click on a test name to see its output) ClangClang.Driver/spirv-target-validation.cIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
|
You should not format code (manually or automatically) outside of your changes. It makes it harder to review your PR. |
Can you elaborate what you mean by this? I attached the clang formatter to my VSCode settings file now so that when I saw it automatically formats, is that not something I should be doing? |
0d88878 to
35dc19d
Compare
What I was saying was you should not format the entire file you are editing, only the parts of the code you have changed. In this case it is better to disable auto formatting in your settings. You can either manually format each part of your changes by selecting one area and use the format feature on it or format what ends up in the diff using one of the appropriate script in the clang-format tools directory. The format job checks only the formatting of what you have changed anyways, not the entire file you have modified. |
|
I would also rewrite the title of the PR by adding a tag of on which sub project this PR is mainly touching (which in this case would be prepending the PR title with |
5627e2b to
4c9d66e
Compare
|
Ok I should have removed the formatting changes on the whole file and just applied it to my changes, and I retested it on my end and it triggered the error, in addition to making the title changes. If there is something else please let me know. |
When a user passes '-target spirv' without specififying a vulkan environment ttriple, SPIRVTargetInfo will fire an assert instead of throwing an error diagnostic. Added this diagnostic in CompilerInstance::createTarget() before target is initialized. Fixes #189964